package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.IncomeBasicInfoMapper;
import com.koocloud.electroplatemanage.mapper.ProcessMapper;
import com.koocloud.electroplatemanage.mapper.ProcessParameterMapper;
import com.koocloud.electroplatemanage.pojo.IncomeBasicInfoVo;
import com.koocloud.electroplatemanage.pojo.Process;
import com.koocloud.electroplatemanage.pojo.ProcessParameter;
import com.koocloud.electroplatemanage.service.OperationCardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: Information_platform
 * @description: 工艺卡片实现层
 * @author: xxc
 * @create: 2020-11-12 16:30
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class OperationCardServiceImpl implements OperationCardService {
    @Resource
    private IncomeBasicInfoMapper incomeBasicInfoMapper;
    @Resource
    private ProcessMapper processMapper;
    @Resource
    private ProcessParameterMapper processParameterMapper;
    Lock lock = new ReentrantLock();

    /**
     *@Description: 通过工单号查询一条工艺卡片信息
     *@Param: [workOrderNumber]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @Override
    public ResponseTemplate selectByWorkOrderNumber(String workOrderNumber) {
        try {
            //创建来件基本信息实体 (工艺卡片左)
            IncomeBasicInfoVo incomeBasicInfoVo = incomeBasicInfoMapper.selectIncomeBasicInfo(workOrderNumber);
            //创建技术要求实体 （工艺卡片右上）
            Process process = processMapper.selectByWorkOrderNumber(workOrderNumber);
            //创建工艺参数实体 （工艺卡片右中）
            ProcessParameter processParameter = processParameterMapper.selectByProcessSpecification(String.valueOf(process.getId()));
            //判断查表操作是否成功
            if (null != incomeBasicInfoVo) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(incomeBasicInfoVo).build();
            } else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    /**
     *@Description: 通过工单号查询一条来件基本信息
     *@Param: [workOrderNumber] 工单号
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/12
     */
    @Override
    public ResponseTemplate selectIncomeBasicInfo(String workOrderNumber) {
        try {
            //创建来件基本信息实体
            IncomeBasicInfoVo incomeBasicInfoVo = incomeBasicInfoMapper.selectIncomeBasicInfo(workOrderNumber);
            //判断查表操作是否成功
            if (null != incomeBasicInfoVo) {
                //查表成功返回数据
                return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(incomeBasicInfoVo).build();
            } else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    /**
     *@Description: 对来件基础信息进行全表查询
     *@Param: [page, limit, incomeBasicInfoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @Override
    public ResponseTemplate selectAllIncomeBasicInfo(String page, String limit, IncomeBasicInfoVo incomeBasicInfoVo) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if(Validator.isNumber(page)&&Validator.isNumber(limit)&&!("0".equals(page)&&"0".equals(limit))) {
            try {
                //开始分页
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                //全局查询
                List<IncomeBasicInfoVo> IncomeBasicInfoVoList = incomeBasicInfoMapper.selectAllData(incomeBasicInfoVo);
                //构造分页实体
                PageInfo<IncomeBasicInfoVo> info=new PageInfo<>(IncomeBasicInfoVoList);
                if (info.getTotal()>0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(info.getTotal()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        } else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }

    /**
     *@Description: 新增一条来件基础信息数据
     *@Param: [incomeBasicInfoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @Override
    public ResponseTemplate insertIncomeBasicInfo(IncomeBasicInfoVo incomeBasicInfoVo) {
        return null;

    }

    /**
     *@Description: 逻辑删除一条来件基础信息记录
     *@Param: [workOrderNumber, deleteFlag]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @Override
    public ResponseTemplate deleteIncomeBasicInfo(String workOrderNumber, String deleteFlag) {
        return null;

    }

    /**
     *@Description: 修改一条来件基础信息记录
     *@Param: [incomeBasicInfoVo]
     *@return: com.koocloud.electroplatemanage.common.pojo.ResponseTemplate
     *@Author: xxc
     *@date: 2020/11/13
     */
    @Override
    public ResponseTemplate updateIncomeBasicInfo(IncomeBasicInfoVo incomeBasicInfoVo) {
        return null;

    }
}
